package com.azusa.xbjy.controller;

import com.azusa.xbjy.entity.Meeting;
import com.azusa.xbjy.entity.Result;
import com.azusa.xbjy.entity.StatusCode;
import com.azusa.xbjy.entity.User;
import com.azusa.xbjy.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("meeting")
public class MeetingController {

    @Autowired
    private MeetingService meetingService;

    @RequestMapping("toMeeting")
    public ModelAndView toMeeting(){
        return new ModelAndView("meeting/meeting");
    }

    @RequestMapping("toMeetingDetail")
    public ModelAndView toMeetingDetail(){
        return new ModelAndView("meeting/meeting_detail");
    }

    @RequestMapping("toMeetingAdd")
    public ModelAndView toMeetingAdd(){
        return new ModelAndView("meeting/meeting_add");
    }


    @PostMapping("findPage/{pageNum}/{pageSize}")
    public Result findPage(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize, @RequestBody Map<String,String> params){
        return new Result(StatusCode.SUCCESS,meetingService.findPage(pageNum, pageSize, params.get("title"), params.get("status")));
    }

    @PostMapping("insertMeeting")
    public Result insertMeeting(@RequestBody Meeting meeting){
        meeting.setPublishDate(new Date());
        meetingService.save(meeting);
        return new Result(StatusCode.SUCCESS);
    }

    @RequestMapping("findById/{id}")
    public Result findById(@PathVariable("id")int id, HttpSession session){
        return new Result(StatusCode.SUCCESS,meetingService.findById(id,(Integer) session.getAttribute("userId")));
    }

    @PutMapping("joinMeeting/{mid}/{uid}/{flag}")
    public Result joinMeeting(@PathVariable("mid")int mid,@PathVariable("uid")int uid,@PathVariable("flag")boolean flag){
        return new Result(StatusCode.SUCCESS,meetingService.joinMeeting(mid,uid,flag));
    }
}
